package com.uwo.commons.configuration.aop.validate.validate;

import com.uwo.commons.configuration.aop.validate.ErrorCode;
import com.uwo.commons.configuration.aop.validate.ValidateField;
import com.uwo.commons.configuration.aop.validate.ValidateProperties;
import com.uwo.commons.configuration.aop.validate.excepteion.ApiValidateExceptrion;
import com.uwo.commons.configuration.aop.validate.annotation.NotNull;
import com.uwo.commons.kits.StringKits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Map;

/**
 * 判断是否为null
 * @author hao.yan
 */
@Component(value = "Validate_NotNull")
public class NotNullValidate extends BaseValidate{

    @Autowired
    private ValidateProperties validateProperties;

    @Override
    public void validate(Annotation annotation, ValidateField validateField) throws ApiValidateExceptrion {
        super.validate(annotation, validateField);
        if(validateField.getValue() == null) {
            NotNull notNull = (NotNull)annotation;
            String message = (notNull == null) ?
                null : value(validateProperties.getMessages(), notNull.message(),
                    ErrorCode.MISSING_FIELD.getMessage(validateField.getName()));

            exception(notNull.code(), message, ErrorCode.MISSING_FIELD);
        }
    }

}
